package com.lyn.policy.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.lyn.common.kit.RandomKit;
import com.lyn.common.kit.SqlKit;
import com.lyn.common.model.Result;
import com.lyn.common.model.ResultList;
import com.lyn.policy.domain.PolicyDoc;

@Service
public class PolicyDocService {
	
	//check
	public Result<PolicyDoc> select(String id) { 
		PolicyDoc policyDoc = PolicyDoc.dao.findById(id);
		if(policyDoc!=null) {
			return new Result<PolicyDoc>(10000, "查询成功", policyDoc);
		} else {
			return new Result<PolicyDoc>(90000, "查询失败", null);
		}
	}
	
	//check
	public Result<ResultList<PolicyDoc>> selects(String ids, Integer pageNumber, Integer pageSize) {
		String sqlSelect = "select id, name, content ";
		String sqlExpect = " from policy_doc ";
		List<Object> params = new ArrayList<Object>();
		if(ids!=null && !"".equals(ids)) {
			sqlExpect = SqlKit.addWhereInSplit(sqlExpect, "id", ids);
		}
		if(pageNumber==null || pageNumber==0) {
			pageNumber = 1;
		}
		if(pageSize==null || pageSize==0) {
			pageSize = 15;
		}
		Page<PolicyDoc> page = PolicyDoc.dao.paginate(pageNumber, pageSize, sqlSelect, sqlExpect, params.toArray());
		ResultList<PolicyDoc> resultList = new ResultList<PolicyDoc>();
		resultList.setItems(page.getList());
		resultList.setPageNum(pageNumber);
		resultList.setPageSize(pageSize);
		resultList.setTotalRows(page.getTotalRow());
		return new Result<ResultList<PolicyDoc>>(10000, "查询文档列表成功", resultList);
	}
	
	//check
	public Result<ResultList<PolicyDoc>> selectParam(String deptname, Integer no, Integer pageNumber, Integer pageSize) {
		String sqlSelect = "select id, name, content ";
		String sqlExpect = " from policy_doc ";
		List<Object> params = new ArrayList<Object>();
		if(StrKit.notBlank(deptname)) {
			sqlExpect = SqlKit.addWhereParam(sqlExpect, "deptname", params, deptname);
		}
		if(no!=null) {
			sqlExpect = SqlKit.addWhereParam(sqlExpect, "no", params, no);
		}
		
		if(pageNumber==null || pageNumber==0) {
			pageNumber = 1;
		}
		if(pageSize==null || pageSize==0) {
			pageSize = 15;
		}
		Page<PolicyDoc> page = PolicyDoc.dao.paginate(pageNumber, pageSize, sqlSelect, sqlExpect, params.toArray());
		ResultList<PolicyDoc> resultList = new ResultList<PolicyDoc>();
		resultList.setItems(page.getList());
		resultList.setPageNum(pageNumber);
		resultList.setPageSize(pageSize);
		resultList.setTotalRows(page.getTotalRow());
		return new Result<ResultList<PolicyDoc>>(10000, "查询文档列表成功", resultList);
	}
	

	
	//check
	public Result<PolicyDoc> insert(PolicyDoc vo) {
		vo.setId(RandomKit.randomStringFixLength(32).toUpperCase());
		vo.setGmtCreate(new Date());
		if(vo.save()) {
			return new Result<PolicyDoc>(10000, "新增文档成功", vo);
		} else {
			return new Result<PolicyDoc>(90000, "新增文档失败", null);
		}
	}
	
	//check
	public Result<ResultList<PolicyDoc>> inserts(List<PolicyDoc> vos) {
		List<PolicyDoc> res = new ArrayList<PolicyDoc>();
		for(PolicyDoc vo : vos) {
			vo.setId(RandomKit.randomStringFixLength(32).toUpperCase());
			vo.setGmtCreate(new Date());
			if(vo.save()) {
				res.add(vo);
			}
		}
		ResultList<PolicyDoc> resultList = new ResultList<PolicyDoc>();
		resultList.setItems(res);
		if(res.size()==vos.size()) {
			return new Result<ResultList<PolicyDoc>>(10000, "新增文档成功", resultList);
		} else {
			return new Result<ResultList<PolicyDoc>>(90000, "部分文档新增失败", resultList);
		}
	}
	
	//check
	public Result<PolicyDoc> update(PolicyDoc vo) {
		PolicyDoc res = PolicyDoc.dao.findById(vo.getId());
		if(res==null) {
			return new Result<PolicyDoc>(90000, "更新文档失败，文档不存在", null);
		} 
		if(vo.getName()!=null) {
			res.setName(vo.getName());
		}
		if(vo.getContent()!=null) {
			res.setContent(vo.getContent());
		}
		res.setGmtModify(new Date());
		if(res.update()) {
			return new Result<PolicyDoc>(10000, "更新文档成功", res);
		} else {
			return new Result<PolicyDoc>(90000, "更新文档失败", null);
		}
	}
	
	//check
	public Result<Integer> updates(List<PolicyDoc> vos) {
		int counter = 0;
		for(PolicyDoc vo : vos) {
			PolicyDoc res = PolicyDoc.dao.findById(vo.getId());
			if(res==null) {
				//logger.error("更新文档失败，文档不存在");
				continue;
			} 
			if(vo.getName()!=null) {
				res.setName(vo.getName());
			}
			if(vo.getContent()!=null) {
				res.setContent(vo.getContent());
			}
			res.setGmtModify(new Date());
			if(res.update()) {
				counter++;
				//logger.error("更新文档成功");
			} else {
				//logger.error("更新文档失败");
			}
		}
		if(counter==vos.size()) {
			return new Result<Integer>(10000, "更新文档成功", counter);
		} else {
			return new Result<Integer>(90000, "部分文档更新失败", counter);
		}
	}
	
	//check
	public Result<Integer> updateParam(PolicyDoc vo, String authorId) {
		String sqlExpect = "update policy_doc ";
		List<Object> params = new ArrayList<Object>();
		
		if(vo.getName()!=null) {
			sqlExpect = SqlKit.addSetParam(sqlExpect, "name" , params, vo.getName());
		}
		if(vo.getContent()!=null) {
			sqlExpect = SqlKit.addSetParam(sqlExpect, "content" , params, vo.getContent());
		}
		if(vo.getDeptname()!=null) {
			sqlExpect = SqlKit.addSetParam(sqlExpect, "deptname" , params, vo.getDeptname());
		}
		if(vo.getNo()!=null) {
			sqlExpect = SqlKit.addSetParam(sqlExpect, "no" , params, vo.getNo());
		}
		sqlExpect = SqlKit.addSetParam(sqlExpect, "gmt_modify", params, new Date());
		if(authorId!=null) {
			sqlExpect = SqlKit.addWhereParam(sqlExpect, "author_id" , params, authorId);
		}
		int counter = Db.update(sqlExpect, params.toArray());
		if(counter > 0) {
			return new Result<Integer>(10000, "更新文档成功", counter);
		} else {
			return new Result<Integer>(90000, "更新文档失败，未更新任何文档", counter);
		}
	}
	

	//check
	public Result<PolicyDoc> delete(String id) {
		if(PolicyDoc.dao.deleteById(id)) {
			return new Result<PolicyDoc>(10000, "删除文档成功", null);
		} else {
			return new Result<PolicyDoc>(90000, "删除文档失败", null);
		}
	}
	
	//check
	public Result<Integer> deletes(String ids) {
		
		String sql = "delete from policy_doc ";
		if(ids!=null && !"".equals(ids)) {
			sql = SqlKit.addWhereInSplit(sql, "id", ids);
		}
		int counter = Db.update(sql);
		
		if(counter==ids.split(",").length) {
			return new Result<Integer>(10000, "删除文档成功", counter);
		} else {
			return new Result<Integer>(90000, "删除文档失败", counter);
		}
	}
	
	//check
	public Result<Integer> deleteParam(String deptname, Integer no) {
		String sqlExpect = "delete from policy_doc ";
		List<Object> params = new ArrayList<Object>();
		if(deptname!=null) {
			sqlExpect = SqlKit.addWhereParam(sqlExpect, "deptname", params, deptname);
		}
		if(no!=null) {
			sqlExpect = SqlKit.addWhereParam(sqlExpect, "no", params, no);
		}
		int counter = Db.update(sqlExpect, params.toArray());
		if(counter > 0) {
			return new Result<Integer>(10000, "删除文档成功", counter);
		} else {
			return new Result<Integer>(90000, "删除文档失败", counter);
		}
		
	}
	
}
